// pages/local/local.js
const app = getApp()
const host = app.globalData.host
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    length: "",
    index:1,
    dataLength:5,
    accountIndex: 0,
    id: 1,
    value: "",
    size:2,
    fbTitle: [],
    pictures: [],
    records: [],
    phoneNum: "",
  },
  //页面初始化
  onShow() {
    var that = this;
    var token = wx.getStorageSync('token')
    wx.showLoading({
      title: '加载中',
    })
    //应急管理标题（缺少接口）
    wx.request({
      url: host + '/yezhu/categorys',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        token: token
      },
      success(res) {
        var msg = res.data.msg;
        wx.hideLoading()
        console.log(res.data.data);
        that.setData({
          fbTitle: res.data.data
        })
      },
      fail(res) {
        var msg = res.data.msg;
        wx.showModal({
          title: '提示',
          content: msg,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    });
    this.getRecord(that.data.index)
  },
  //显示下拉列表
  showSelect() {
    var that = this;
    this.setData({
      isSelect: !that.data.isSelect
    });
  },
  // 点击下拉列表
  bindAccountChange: function (e) {
    var that = this;
    console.log('picker account 发生选择改变，携带值为', e.currentTarget.dataset.accountindex);
    this.setData({
      accountIndex: e.currentTarget.dataset.accountindex,
      isSelect: !that.data.isSelect
    })
  },
  //获取记录
  getRecord(index) {
    var that = this;
    var token = wx.getStorageSync('token')
    var code = wx.getStorageSync("code");
    wx.request({
      url: host + '/yezhu/records',
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      data: {
        code: code,
        token: token,
        page_num: index,
        size: 2
      },

      success(res) {
        console.log(res.data);
        wx.hideLoading();
        for (let c in res.data.data) {
          let date = util.formatTime(res.data.data[c].add_time)
          res.data.data[c].add_time = date
        }
        if (res.data.code == 0) {
          that.setData({
            records: that.data.records.concat(res.data.data),
            dataLength:res.data.data.length,
            total: res.data.data[0].total
          })
        } else {
          app.globalData.isShow(res.data.msg)
        }
      },
      fail(res) {
        app.globalData.isShow(res.data.msg)
      }
    })
  },
  //上传图片
  chooseImage: function (e) {
    var that = this;
    if (that.data.pictures.length < 4) {
      wx.chooseImage({
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          console.log(res);
          var tempFilePaths = res.tempFilePaths[0]
          var tempFiles = res.tempFiles[0];
          console.log(tempFilePaths[0]);
          wx.uploadFile({
            url: host + '/files/ossfile',
            method: 'POST',
            header: {
              'content-type': 'application/json'
            },
            filePath: tempFilePaths,
            name: "file",
            success: function (res) {
              var data = JSON.parse(res.data);
              if(data.code == 0){
                that.setData({
                  pictures: that.data.pictures.concat(data.data.file_url)
                })
              }else{
                app.globalData.isShow(data.msg)
              }
            },
            fail(res) {
              app.globalData.isShow(JSON.parse(res.data).msg)
            }
          })
        }
      })
    } else {
      app.globalData.isShow("上传图片不能超过4张！");
      return
    }
  },
  previewImage: function (e) {
    console.log(e.currentTarget);
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.pictures // 需要预览的图片http链接列表
    })
  },
  //添加评论
  getContent(e) {
    this.setData({
      value: e.detail.value
    })
  },
  //提交数据
  submit() {
    var that = this;
    var token = wx.getStorageSync('token')
    wx.request({
      url: host + '/yezhu/records',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: token,
        yezhu_category_id: that.data.fbTitle[that.data.accountIndex].id,
        title: that.data.fbTitle[that.data.accountIndex].title,
        yezhu_img_url: that.data.pictures,
        thinking: that.data.value
      },
      success(res) {
        if (res.data.code == 0) {
          wx.showModal({
            title: '提示',
            content: '数据提交成功',
            success: function (res) {
              if (res.confirm) {
                that.setData({
                  pictures: [],
                  records: []
                })
                that.getRecord(1);
              }
            }
          })
        } else {
          app.globalData.isShow(res.data.msg)
        }
      },
      fail(res) {
        app.globalData.isShow(res.data.msg)
      }
    })
  },
  //上拉触底
  //上拉触底
  onReachBottom: function () {
    var that = this;
    var flag = parseInt(that.data.total / that.data.size) + 1;
    wx.showLoading({
      title: '加载中',
    });
    if (that.data.index < flag) {
      that.data.index += 1;
      that.getRecord(that.data.index);
      console.log(that.data.index);
    } else {
      wx.hideLoading();
      return
    }
  },
})